﻿
using System;
using System.Timers;

public class TickEventArgs : EventArgs
{
    public DateTime CurrentTime { get; set; }
}

public class AlarmEventArgs : EventArgs
{
    public DateTime AlarmTime { get; set; }
}

public class AlarmClock
{
    private readonly System.Timers.Timer _timer;
    public event EventHandler<TickEventArgs> Tick;
    public event EventHandler<AlarmEventArgs> Alarm;

    public DateTime AlarmTime { get; set; }

    public AlarmClock()
    {
        _timer = new System.Timers.Timer(1000); // 1秒间隔
        _timer.Elapsed += TimerElapsed;
    }

    public void Start() => _timer.Start();
    public void Stop() => _timer.Stop();

    private void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        var now = DateTime.Now;
        Tick?.Invoke(this, new TickEventArgs { CurrentTime = now });

        if (now >= AlarmTime)
        {
            Alarm?.Invoke(this, new AlarmEventArgs { AlarmTime = AlarmTime });
            Stop();
        }
    }
}

class ConsoleProgram
{
    static void Main()
    {
        var alarmClock = new AlarmClock
        {
            AlarmTime = DateTime.Now.AddSeconds(5)
        };

        alarmClock.Tick += (s, e) =>
            Console.WriteLine($"滴答: {e.CurrentTime:HH:mm:ss}");

        alarmClock.Alarm += (s, e) =>
            Console.WriteLine($"\a闹铃! 设定时间: {e.AlarmTime:HH:mm:ss}");

        alarmClock.Start();
        Console.WriteLine("按任意键退出...");
        Console.ReadKey();
    }
}
